//
//  MyCollectionViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/4/10.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "InvestTableViewCell.h"
#import "BidViewController.h"
#import "CollectionCell.h"
static NSString * const InvestTableViewCellReuseId = @"InvestTableViewCell";
static NSString * const CollectionCellReuseId = @"CollectionCell";
@interface MyCollectionViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL shouldEit;
    
    UIButton *chooseAllBtn;
//    BOOL chooseAll;
}
@property(nonatomic,strong) UIView *bottomView;
@property (nonatomic, strong) UITableView *tableView;
/** 消息数据 */
@property (nonatomic,strong) NSMutableArray *dataArr;

//选中状态
@property (nonatomic,strong) NSMutableArray *chooseArr;

//选中数组
@property (nonatomic,strong) NSMutableArray *selectArr;
@end

@implementation MyCollectionViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的收藏";
    [self.view addSubview:self.tableView];
    
    [self registerCell];
    
//    _page = 1;
    
    [self addRightButton];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //    [self.comBoBoxView dimissPopView];
    self.navigationController.navigationBarHidden = NO;
    [WB_SharedApplication setStatusBarStyle:UIStatusBarStyleLightContent];
} 

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}


- (void)addRightButton
{
    UIButton *btn = [UIButton ba_buttonWithFrame:CGRectMake(0, 0, 80, 64) title:@"管理" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:13]];
    [btn ba_button_setButtonLayoutType:BAKit_ButtonLayoutTypeRightImageLeft padding:2.0f];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    btn.frame = CGRectMake(0, 0, 80, 64);
    //把视图的边角变为圆形, cornerRadius圆角半径
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    //弹簧控件, 修复边距
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -5;
    self.navigationItem.rightBarButtonItems = @[spaceItem,backItem];
    [btn addTarget:self action:@selector(clickRule:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)clickRule:(UIButton *)sender
{
    sender.selected = !sender.selected;
    shouldEit = sender.selected;
    
    if (shouldEit) {
        [sender setTitle:@"取消" forState:UIControlStateNormal];
        //[self.tableView setEditing:YES animated:YES];
        
        [self.view addSubview:self.bottomView];
//        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, self.bottomView.mm_height, 0);
        self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationBarHeight-self.bottomView.mm_height);
    }else{
        [sender setTitle:@"管理" forState:UIControlStateNormal];
        //[self.tableView setEditing:NO animated:YES];
//        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [self.bottomView removeFromSuperview];
        self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationBarHeight);
    };
    
//    if (btn.selected) {
//        [self.selectArr addObjectsFromArray:self.dataArr];
//    }else{
//        [self.selectArr removeAllObjects];
//    }
    [chooseAllBtn setTitle:[NSString stringWithFormat:@"全选（%lu）",(unsigned long)self.selectArr.count] forState:UIControlStateNormal];
    chooseAllBtn.selected = YES;
    [self.tableView reloadData];
}

//选择所有
- (void)chooseAll:(UIButton*)btn
{
    btn.selected = !btn.selected;
    
    for (int i = 0; i<self.chooseArr.count; i++) {
        
        [self.chooseArr replaceObjectAtIndex:i withObject:@(btn.selected)];
    }
    
    [self.selectArr removeAllObjects];
    if (btn.selected) {
        
        [self.selectArr addObjectsFromArray:self.dataArr];
    }
    
    [chooseAllBtn setTitle:[NSString stringWithFormat:@"全选（%lu）",(unsigned long)self.selectArr.count] forState:UIControlStateNormal];
    
    [self.tableView reloadData];
}
//删除选中
- (void)deleteChoose
{
    
}
- (void)registerCell
{
    [self.tableView registerNib:[UINib nibWithNibName:InvestTableViewCellReuseId bundle:nil] forCellReuseIdentifier:InvestTableViewCellReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:CollectionCellReuseId bundle:nil] forCellReuseIdentifier:CollectionCellReuseId];
}

#pragma mark - 懒加载  Lazy Load
- (UIView*)bottomView
{
    if (!_bottomView) {
        
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight-kNavigationBarHeight-60, kScreenWidth, 60)];
        
        chooseAllBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, _bottomView.mm_height)];
        [_bottomView addSubview:chooseAllBtn];
        chooseAllBtn.titleLabel.font = WB_FontSize(15);
        [chooseAllBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [chooseAllBtn setTitle:[NSString stringWithFormat:@"全选（%lu）",(unsigned long)self.selectArr.count] forState:UIControlStateNormal];
        [chooseAllBtn addTarget:self action:@selector(chooseAll:) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(chooseAllBtn.mm_right, 10, 1, _bottomView.mm_height-20)];
        line.backgroundColor = [UIColor lightGrayColor];
        [_bottomView addSubview:line];
        
        
        UIButton *delete = [[UIButton alloc]initWithFrame:CGRectMake(line.mm_right, 0, kScreenWidth-line.mm_right, _bottomView.mm_height)];
        [delete setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [delete setTitle:@"删除收藏" forState:UIControlStateNormal];
        delete.titleLabel.font = WB_FontSize(15);
        [_bottomView addSubview:delete];
        [delete addTarget:self action:@selector(deleteChoose) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bottomView;
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationBarHeight) style:UITableViewStylePlain];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.allowsSelectionDuringEditing = YES;
        _tableView.allowsMultipleSelectionDuringEditing = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
//        if (@available(iOS 11.0, *)) {
//           // _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//            _tableView.estimatedRowHeight = 0;
//        }
        
    }
    return _tableView;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.dataArr.count ? self.dataArr.count : 1;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { 
    if (self.dataArr.count) {
        InvestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:InvestTableViewCellReuseId forIndexPath:indexPath];
       // cell.data = _dataArr[indexPath.row];
        cell.bidClick = ^{
            BidViewController *vc = [[BidViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        };
        

        NSDictionary *dic = self.dataArr[indexPath.row];
        if (shouldEit) {

            cell.isEdit = YES;
            
            cell.chooseBtn.selected = [self.chooseArr[indexPath.row]boolValue];
            
            cell.chooseBack = ^(BOOL selected){
              
                
                [self.chooseArr replaceObjectAtIndex:indexPath.row withObject:@(selected)];
                
                if (selected) {
                    
                    [self.selectArr addObject:dic];
                }else{
                    [self.selectArr removeObject:dic];
                    
                }
                [chooseAllBtn setTitle:[NSString stringWithFormat:@"全选（%lu）",(unsigned long)self.selectArr.count] forState:UIControlStateNormal];
            };
           
        }else{
            cell.isEdit = NO;
        }

        
        return cell;
    }else{
        CollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:CollectionCellReuseId forIndexPath:indexPath];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.dataArr.count ?  135.f : kScreenHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   

}


- (NSMutableArray *)dataArr{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
        _dataArr = @[@{@"1":@"2"},@{@"2":@"2"},@{@"3":@"2"},@{@"4":@"2"},@{@"5":@"2"},@{@"6":@"2"},@{@"7":@"2"},@{@"8":@"2"}].mutableCopy;
        
        
    }
    return _dataArr;
}
- (NSMutableArray *)chooseArr
{
    if (!_chooseArr) {
        _chooseArr = [NSMutableArray array];
        _chooseArr = @[@(YES),@(YES),@(YES),@(YES),@(YES),@(YES),@(YES),@(YES) ].mutableCopy;
    }
    return _chooseArr;
}

- (NSMutableArray *)selectArr
{
    if (!_selectArr) {
//        _selectArr = [NSMutableArray array];
        _selectArr = [self.dataArr mutableCopy];
    }
    return _selectArr;
}
@end
